package com.meilai.project.mapper.business.installOrder;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.business.installOrder.WorkerRepairPointDTO;
import com.meilai.project.dto.business.installOrder.WorkerSubmitListDTO;
import com.meilai.project.entity.business.installOrder.WorkerSubmit;
import com.meilai.project.vo.business.installOrder.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 工人提交 Mapper 接口
 * </p>
 *
 * @author 张驰
 * @since 2022-03-21
 */
@Mapper
public interface WorkerSubmitMapper extends BaseMapper<WorkerSubmit> {
    String getDetailByWSID(@Param("workerSubmitId") Long workerSubmitId);

    List<WorkerSubmit> listByWorkOrderIds(@Param("workOrderIds") List<Long> workOrderIds);

    List<WorkerRepairPointVO> selectListNeedRepairPoint(Page<WorkerRepairPointVO> page, @Param("query") WorkerRepairPointDTO workerRepairPointDTO);

    int listTotal();

    @Select("SELECT a.* " +
            "FROM worker_submit a " +
            "JOIN work_order b ON a.work_order_id=b.id " +
            "WHERE a.deleted_at IS NULL AND a.flow_status IN (2,3) " +
            "AND b.work_order_type_id=#{work_order_type_id} " +
            "AND a.media_point_id=#{media_point_id} " +
            "ORDER BY a.created_at DESC")
    List<WorkerSubmit> getWorkOrderCurTypePointSubmit(@Param("work_order_type_id") Integer work_order_type_id,
                                                      @Param("media_point_id") Long media_point_id);

    @Select("SELECT a.* " +
            "FROM worker_submit a " +
            "JOIN work_order b ON a.work_order_id=b.id " +
            "WHERE a.deleted_at IS NULL " +
            "AND b.id=#{work_order_id}  " +
            "AND b.work_order_type_id=#{work_order_type_id} " +
            "AND a.media_point_id=#{media_point_id} " +
            "ORDER BY a.created_at DESC")
    List<WorkerSubmit> getWorkOrderCurTypePointLatestSubmit(@Param("work_order_id") Long work_order_id,
            @Param("work_order_type_id") Integer work_order_type_id, @Param("media_point_id") Long media_point_id);

    @Select("SELECT a.*  " +
            "FROM worker_submit a  " +
            "JOIN work_order b ON a.work_order_id=b.id  " +
            "WHERE a.deleted_at IS NULL AND a.flow_status IN (2,3)  " +
            "AND b.id=#{work_order_id}  " +
            "AND a.media_point_id=#{media_point_id} " +
            "ORDER BY a.created_at DESC ")
    List<WorkerSubmit> getWorkOrderPointSubmit(@Param("work_order_id") Long work_order_id,
                                                      @Param("media_point_id") Long media_point_id);

    @Select("SELECT a.*  " +
            "FROM worker_submit a  " +
            "JOIN work_order b ON a.work_order_id=b.id  " +
            "WHERE a.deleted_at IS NULL " +
            "AND b.id=#{work_order_id}  " +
            "AND a.media_point_id=#{media_point_id} " +
            "ORDER BY a.created_at DESC ")
    List<WorkerSubmit> getWorkOrderPointLatestSubmit(@Param("work_order_id") Long work_order_id,
                                               @Param("media_point_id") Long media_point_id);

    @Select("select ws.flow_status as statusEnum, count(ws.id) as statusCount  " +
            "from worker_submit ws  " +
            "join work_order wo on ws.work_order_id = wo.id " +
            "where  " +
            "wo.deleted_at is null  " +
            "and wo.id = #{work_order_id} " +
            "and ws.deleted_at is null " +
            "and ws.`status` = 1 " +
            "and ws.flow_status in (2,3,4) " +
            "GROUP BY ws.flow_status")
    List<WorkerSubmitStatusCountVO> getWorkerSubmitStatusCount(
            @Param("work_order_id") Long work_order_id);

    @Select("select -1 as statusEnum, count(ws.id) as statusCount from worker_submit ws  " +
            "join work_order wo on ws.work_order_id = wo.id " +
            "where  " +
            "wo.deleted_at is null  " +
            "and wo.id = #{work_order_id} " +
            "and ws.deleted_at is null " +
            "and ws.`status` = 1 " +
            "and ws.flow_status in (2,3,4) " +
            "and ws.media_point_id in ( " +
            " select media_point_id from worker_submit " +
            " where work_order_id = #{work_order_id} " +
            " and worker_submit.deleted_at is null " +
            " and worker_submit.`status` = 1 " +
            " and worker_submit.flow_status in (2,3,4) " +
            " group by media_point_id HAVING count(media_point_id) > 1 " +
            ")")
    WorkerSubmitStatusCountVO getMultiWorkerSubmitStatusCount(@Param("work_order_id") Long work_order_id);


    List<WorkerSubmitVO> workerSubmitCheckList(Page<WorkerSubmitVO> page, @Param("query") WorkerSubmitListDTO workerSubmitListDTO);

    List<WorkerSubmitVO> multiWorkerSubmitCheckList(Page<WorkerSubmitVO> page, @Param("query") WorkerSubmitListDTO workerSubmitListDTO);

    int updateCheckStatus(@Param("status") Integer status, @Param("ids") List<Long> ids);

    int updateRepairStatus(@Param("work_order_id") Long work_order_id, @Param("ids") List<Long> ids);

    int markDelete(@Param("ids") List<Long> ids);

    List<WorkerSubmitVO> page(@Param("page") Page<WorkerSubmitVO> page, @Param("begin_time") LocalDateTime begin_time, @Param("end_time") LocalDateTime end_time, @Param("work_order_id") Long work_order_id, @Param("worker_id") Long worker_id, @Param("work_order_type_ids") List<Integer> work_order_type_ids, @Param("status") Integer status, @Param("code") String code, @Param("building_area_id") Long building_area_id, @Param("media_type_id") Long media_type_id,@Param("statistics") Boolean statistics, @Param("begin_time_save") LocalDateTime begin_time_save, @Param("end_time_save") LocalDateTime end_time_save);

    List<WorkerSubmitVO> page(@Param("begin_time") LocalDateTime begin_time, @Param("end_time") LocalDateTime end_time, @Param("work_order_id") Long work_order_id, @Param("worker_id") Long worker_id, @Param("work_order_type_ids") List<Integer> work_order_type_ids, @Param("status") Integer status, @Param("code") String code, @Param("building_area_id") Long building_area_id, @Param("media_type_id") Long media_type_id,@Param("statistics") Boolean statistics, @Param("begin_time_save") LocalDateTime begin_time_save, @Param("end_time_save") LocalDateTime end_time_save);

    /**
     * 安装统计表
     *
     * @param page
     * @param startDate
     * @param endDate
     * @param worker_id
     * @param media_type_id
     * @param work_order_type_id
     * @return
     */
    List<WorkerSubmitCountVO> installCountPage(@Param("page") Page<WorkerSubmitCountVO> page, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate, @Param("worker_id") Long worker_id, @Param("media_type_id") Long media_type_id, @Param("work_order_type_id") Long work_order_type_id);


    /**
     * 估计时间统计提交数据
     *
     * @param startTime
     * @param endTime
     * @return
     */
    List<WorkerSubmitCountVO2> installCountByTime(@Param("startTime") LocalDateTime startTime,
                                                  @Param("endTime") LocalDateTime endTime,
                                                  @Param("statusList") List<Integer> statusList,
                                                  @Param("worker_id")Long worker_id,
                                                  @Param("media_type_id")Long media_type_id,
                                                  @Param("work_order_type_id") Long work_order_type_id);


    /**
     * 上画明细统计分页
     *
     * @param page
     * @param startDate
     * @param endDate
     * @param worker_id
     * @param building_area_id
     * @param work_order_type_id
     * @param customer_id
     * @return
     */
    List<WorkerSubmitVO3> installCountRecord(@Param("page") Page<WorkerSubmitVO3> page, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate, @Param("worker_id") Long worker_id, @Param("building_area_id") Long building_area_id, @Param("work_order_type_id") Long work_order_type_id, @Param("customer_id") Long customer_id);

    List<WorkerSubmitCountVO2> getSubmitCountVO2List(@Param("ids") List<Long> order_ids);

    /**
     * 查询所有设备保障次数
     * @return
     */
    List<RepairCountVO> queryDeviceReportCount();
}
